import feedbackZh from '../i18n/feedback/feedback-zh.js';
import feedbackEn from '../i18n/feedback/feedback-en.js';
export default class Util {
    /**
     * date format
     * @param {Date} date => new Date()
     * @param {String} type => 'date'(default)/'time'/'pureTime'
     */
    dateFormat(date, type = 'date') {
        if (date instanceof Date) {
            const mat = {};

            mat.Y = date.getFullYear();
            mat.M = this._check(date.getMonth() + 1); // Month+1
            mat.D = this._check(date.getDate());
            mat.H = this._check(date.getHours());
            mat.m = this._check(date.getMinutes());
            mat.s = this._check(date.getSeconds());

            let result;

            switch (type) {
                case 'time':
                    result = `${mat.Y}-${mat.M}-${mat.D} ${mat.H}:${mat.m}:${mat.s}`;
                    break;
                case 'pureTime':
                    result = `${mat.H}:${mat.m}:${mat.s}`;
                    break;
                default:
                    result = `${mat.Y}-${mat.M}-${mat.D}`;
                    break;
            }
            return result;
        } else if (typeof date === 'number') {
            try {
                const dateForm = new Date(date);
                const Y = dateForm.getFullYear();
                const M = this._check(dateForm.getMonth() + 1); // Month+1
                const D = this._check(dateForm.getDate());
                const H = this._check(dateForm.getHours());
                const m = this._check(dateForm.getMinutes());
                const s = this._check(dateForm.getSeconds());
                let result;

                switch (type) {
                    case 'time':
                        result = `${Y}-${M}-${D} ${H}:${m}:${s}`;
                        break;
                    case 'pureTime':
                        result = `${H}:${m}:${s}`;
                        break;
                    default:
                        result = `${Y}-${M}-${D}`;
                        break;
                }
                return result;
            } catch (error) {
                console.error('date params error!', date, error);
            }
        } else {
            return date;
        }
    }
    // 检查是不是两位数字，不足补全
    _check(str) {
        str = str.toString();
        if (str.length < 2) {
            str = '0' + str;
        }
        return str;
    }

    // 深度复制对象
    deepClone(target) {
        const ctx = this;
        let result = {};

        if (typeof target === 'object') {
            if (target instanceof Array) {
                result = [];
            } else if (target instanceof Date) {
                result = new Date(target);
            }

            for (const key in target) {
                result[key] = typeof target[key] === 'object' ? ctx.deepClone(target[key]) : target[key];
            }
        } else {
            result = target;
        }
        return result;
    }

    randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
    }

    /**
     * 生成随机code
     * @param {String} o -> code resource
     * @param {Number} len -> create code length
     * @return {String}
     */
    makeCode(o, len) {
        let code = '';
        for (let i = 0; i < len; i++) {
            code += o[this.randomNum(0, o.length)];
        }
        return code;
    }

    // 毫秒数 转 天 小时 分钟 秒
    formatDuring(mss) {
        try {
            var days = parseInt(mss / (1000 * 60 * 60 * 24));
            var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = parseInt(((mss % (1000 * 60)) / 1000));
            return [
                this._check(days), this._check(hours), this._check(minutes), this._check(seconds),
            ];
        } catch (error) {
            console.error('formatDuring error_>', error);
            return [];
        }
    }

    getCurrLanguage(store) {
        return store.state.slangChange || 'EN';
    }

    // 读取反馈代码
    judgeLanguage(lang, key) {
        switch (lang) {
            case 'zh':
                return feedbackZh[key];
                break;
            default:
                return feedbackEn[key];
                break;
        }
    }

    // 保留两位有效数字
    toDecimal2(x) {
        const f = parseFloat(x);
        if (isNaN(f)) {
            return x;
        }
        const s = f.toString();
        const rs = s.indexOf('.');
        if (rs < 0) {
            return s;
        }
        return s.substring(0, rs + 3);
    }

    // 随机数据
    shuffle(arr) {
        let i = arr.length;
        while (i) {
            const j = Math.floor(Math.random() * i--);
            [
                arr[j],
                arr[i],
            ] = [
                arr[i],
                arr[j],
            ];
        }
        return arr;
    }
}
